package com.css.fxfzcpxx.modules.informationDelivery.service.impl;

import com.css.fxfzcpxx.base.attachment.service.AttachmentInfoService;
import com.css.fxfzcpxx.common.utils.UpdateUtil;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.informationAchievement.entity.InfAchievement;
import com.css.fxfzcpxx.modules.informationAchievement.vo.InfAchievementVo;
import com.css.fxfzcpxx.modules.informationDelivery.param.ClaCasParams;
import com.css.fxfzcpxx.modules.informationDelivery.vo.ListVo;
import com.css.fxfzcpxx.modules.informationDelivery.entity.InfDelivery;
import com.css.fxfzcpxx.modules.informationDelivery.param.QueParams;

import com.css.fxfzcpxx.modules.informationDelivery.repository.InfDeliveryRepository;
import com.css.fxfzcpxx.modules.informationDelivery.service.InfDeliveryService;
import com.css.fxfzcpxx.modules.informationDelivery.vo.InfDeliveryVo;
import com.css.fxfzcpxx.util.EasyPoiExcelUtil;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import com.css.fxfzcpxx.util.UUIDGenerator;
import com.css.fxfzcpxx.web.PlatformSessionContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InfDeliveryServiceImpl implements InfDeliveryService {
    @Resource
    InfDeliveryRepository infDeliveryRepository;

    @Resource
    AttachmentInfoService attachmentInfoService;

    @Override
    public Map<String, Object> getPage(QueParams queParams, int curPage, int pageSize) {
        Map<String, Object> page = infDeliveryRepository.getPage(queParams,curPage, pageSize);
        return page;
    }

    @Override
    public void save(InfDelivery infDelivery) {
        infDelivery.setId(UUIDGenerator.getUUID());
        infDelivery.setCreateUser(PlatformSessionContext.getUserID());
        infDelivery.setCreateTime(new Date());
        infDelivery.setDelFlag(YNEnum.N.toString());
        infDelivery.setReleaseStatus(YNEnum.Y.toString());
        infDeliveryRepository.save(infDelivery);
    }

    @Override
    public void recallOrPublish(QueParams queParams) {
        infDeliveryRepository.recallOrPublish(queParams.getReleaseStatus(), Arrays.asList(queParams.getIds().split(",")));
    }

    @Override
    public void delete(String ids) {
        infDeliveryRepository.delete(Arrays.asList(ids.split(",")));

    }

    @Override
    public InfDeliveryVo findById(String id) {
        InfDeliveryVo infDeliveryVo=new InfDeliveryVo();
        InfDelivery infDelivery=infDeliveryRepository.findById(id);
        UpdateUtil.copyNullProperties(infDelivery, infDeliveryVo);
        if (PlatformObjectUtils.isNotEmpty(infDelivery.getMeansUpload())){
            infDeliveryVo.setMeansUploadAtt(attachmentInfoService.queryFile(infDelivery.getMeansUpload()));
        }

        return infDeliveryVo;
    }

    @Override
    public void update(InfDelivery infDelivery) {
        infDelivery.setUpdateTime(new Date());
        infDelivery.setUpdateUser(PlatformSessionContext.getUserID());

        if("0".equals(infDelivery.getReleaseStatus())){
            infDeliveryRepository.publishedDataupdate(infDelivery);
        }else{
            infDeliveryRepository.unpublishedDataupdate(infDelivery);
        }
    }

    @Override
    public void catalogDownload(HttpServletResponse response, String ids) {
        List<InfDelivery> infDelivery = infDeliveryRepository.getDataById(Arrays.asList(ids.split(",")));
        if (infDelivery.size() <= 0) {
            throw new RuntimeException("数据为空,无法导出！");
        }
        try {
            EasyPoiExcelUtil.exportExcel(infDelivery, "知识学习数据信息统计", "知识学习数据信息", InfDelivery.class, "知识学习数据信息", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<InfDelivery> getMoveData(QueParams params) {
        return infDeliveryRepository.getMoveData(params);

    }

    @Override
    public void meansTypeUpdate(ListVo listvo) {
        List<InfDelivery> list = listvo.getList();
        for(InfDelivery item:list){
            infDeliveryRepository.meansTypeUpdate(item.getId(),item.getMeansTypeId());
        }
    }

    @Override
    public Map<String, Object> getClaCasPage(ClaCasParams claCasParams, int curPage, int pageSize) {
        Map<String, Object> page = infDeliveryRepository.getClaCasPage(claCasParams,curPage, pageSize);
        List<InfDeliveryVo> rows = (List<InfDeliveryVo>) page.get("rows");
        for(InfDeliveryVo item:rows){
            if (PlatformObjectUtils.isNotEmpty(item.getMeansUpload())){
                item.setMeansUploadAtt(attachmentInfoService.queryFile(item.getMeansUpload()));
            }
        }
        return page;
    }

    @Override
    public List<InfDeliveryVo> getLatestInformation() {
        List<InfDeliveryVo> list = infDeliveryRepository.getLatestInformation();
        for(InfDeliveryVo item:list){
            if (PlatformObjectUtils.isNotEmpty(item.getMeansUpload())){
                item.setMeansUploadAtt(attachmentInfoService.queryFile(item.getMeansUpload()));
            }
        }
        return list;
    }
}
